<template>
  <div class="main01">
    <basic-layout title="体检机构详情" backBtnType="arrow" :showAddBtn="false" class="wrapBox">

      <div class="body_box">
        <div class="main">
          <div style="margin: 10px">
            <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white" v-if="images.length != 0">
              <van-swipe-item v-for="(image,index) in images" :key="index">
                <img class="swiper-img" :src="image.url" alt="">
              </van-swipe-item>
            </van-swipe>
          </div>

          <div style=" font-weight: bold;margin: 5px 10px;">体检机构</div>
          <div>
            <div class="fContentMain firstClass" @click="jumpToDetail()">
              <div class="contentC">
<!--                <div class="box-title">体检机构</div>-->
                <div>
                  <div class="title">
                    <div>体检机构类型</div>
                    <div>{{detailDate.type}}</div>
                  </div>
                  <div class="title">
                    <div>体检机构名称</div>
                    <div>{{detailDate.name}}</div>
                  </div>
                  <div class="title">
                    <div>体检机构地址</div>
                    <div>{{detailDate.address}}</div>
                  </div>
                  <div class="title">
                    <div>联系人</div>
                    <div>{{detailDate.contactName}}</div>
                  </div>
                  <div class="title">
                    <div>联系方式</div>
                    <div>{{detailDate.contactPhone}}</div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div style=" font-weight: bold;margin: 5px 10px;">体检套餐</div>
        <div class="fContentMain secondClass" v-for="(combo,index) in comboList" :key="index" >
          <div class="contentC" >
            <div class="title" >
              <div>套餐名称</div>
              <div>{{combo.name}}</div>
            </div>
            <div class="title" >
              <div>套餐价格</div>
              <div>{{combo.price}}</div>
            </div>
            <div class="title" >
              <div>适用人群</div>
              <div>{{combo.rvHealthPopulation.name}}</div>
            </div>
            <div class="title" >
              <div>年龄分布</div>
              <div>{{combo.rvHealthYear.name}}</div>
            </div>
            <div class="title" >
              <div>操作</div>
              <div class="van-ellipsis" >
                <van-button class="btn" size="small" color="#4eb3e9" @click.stop="handleDetail(combo)">详情</van-button>
<!--                <van-button class="btn" size="small" color="#4eb3e9" @click.stop="handleYuyue(combo)">预约</van-button>-->
              </div>
            </div>
          </div>
        </div>

      </div>
    </basic-layout>

    <!--        预约-->
    <van-popup v-model="showModal">
      <div class="sign-box update-box">

        <van-form class="filterBox">
          <label>预约时间</label>
          <van-field clearable v-model="formData.date" placeholder="请选择状态预约时间" @click="showData = true"/>
        </van-form>

        <van-form class="filterBox">
          <label>预约时间段</label>
          <van-field clickable
                     readonly
                     name="picker"
                     :value="formData.periodName"
                     placeholder="请选择预约时间段"
                     @click="showPeriod = true"/>
        </van-form>

        <van-form class="filterBox">
          <label>备注</label>
          <van-field clearable v-model="formData.remark" placeholder="请输入备注" />
        </van-form>

        <div class="showUpdateWin-btn" style="display: flex;">
          <van-button size="small" class="btn" @click="cancel">取消</van-button>
          <van-button size="small" class="btn" color="#4eb3e9" @click="submit">提交</van-button>
        </div>
      </div>
    </van-popup>

    <!--      请选择预约时间-->
    <van-calendar :min-date="minDate" :max-date="maxDate" :allow-same-day="true" :show-confirm="false"
                  v-model="showData" @confirm="selectData" />

    <!--      请选择预约时间段-->
    <van-popup v-model="showPeriod" position="bottom">
      <van-picker title="请选择预约时间段"
                  show-toolbar
                  :columns="periodList"
                  @confirm="selectPeriod"
                  @cancel="showPeriod = false">
        <template #option="option">
          <div style="display: flex; flex-direction: column; align-items: center;">
            <div>{{ option.name }}</div>
          </div>
        </template>
      </van-picker>
    </van-popup>

    <full-loading v-if="loading"></full-loading>
  </div>
</template>
<script>
import { Dialog } from 'vant';
import { ImagePreview } from 'vant';
import { queryFileList } from "@api/common";
import {getByUserIdAndYear} from "@/api/realAssets/rvhealth/employeeQuota";
import { getById } from "@/api/realAssets/rvhealth/organizations";
import { listCombo } from "@/api/realAssets/rvhealth/combo";
import { addBooking } from "@/api/realAssets/rvhealth/booking";
import {listPeriod } from "@/api/realAssets/rvhealth/period";

export default {
  name: 'applyOrganizationsDetail',
  props: {},
  components: {},
  data() {
    return {
      loading: false,

      //员工信息
      employee: {},

      //体检机构照片
      images: [],
      //体检机构信息
      detailDate: {},

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        organizationsId: this.$route.query.id, //体检机构ID
        status: "正常", //状态
      },
      // 体检套餐表格数据
      comboList: [],
      // 体检预约时间段数据
      periodList: [],

      //=============================预约============================
      // 展示
      showModal: false,

      // 表单参数
      formData: {
        userId: null,
        comboId: null,
        price: null,
        date: null,
        periodId: null,
        periodName: null,
        remark: null
      },

      //选择预约时间段
      showPeriod: false,
      //预约时间展示
      minDate: new Date(),
      maxDate: new Date(new Date().getFullYear() + 1, 11, 31),
      showData: false,

    }
  },

  created() {
    // 根据用户标识id 和 年度查询
    this.getEmployee();
    //根据id获得体检机构信息
    this.getByOrganizationsId();
    // 体检套餐表格数据
    this.getCombo();
    //查询体检预约时间段
    this.getPeriod();
  },
  computed: {},

  methods: {
    // 根据用户标识id 和 年度查询
    getEmployee(){
      // 根据用户标识id 和 年度查询
      this.loading = true;
      getByUserIdAndYear({
        userId: JSON.parse(sessionStorage.getItem("userInfo")).userId,
        year: new Date().getFullYear()
      }).then(response => {
        this.employee = response.data.data;
        this.loading = false;
      });
    },
    // 根据id获得体检机构信息
    getByOrganizationsId() {
      //根据id获得体检机构信息
      getById({id: this.$route.query.id}).then(response => {
        //体检机构信息
        this.detailDate = response.data.data;

        //走马灯的轮播图
        this.images = [];
        //获得照片数据
        if (this.detailDate.fileArr.length != 0) {
          //根据fileCodeArr获得url
          queryFileList(this.detailDate.fileArr).then((res) => {
            if (res.code === 1000 && res.data) {
              //走马灯的轮播图
              this.images = res.data;
            }
          });
        }

      });
    },
    // 体检套餐表格数据
    getCombo() {
      listCombo(this.queryParams).then(response => {
        this.comboList = response.data.data.list;
      });
    },
    //查询体检预约时间段
    getPeriod(){
      listPeriod().then(response => {
        this.periodList = response.data.data;
      });
    },


    //预约
    handleYuyue(row){
      if (this.employee == null){
        this.$Notify({type: 'danger', message: '请联系管理员进行体检预约！'});
        return false;
      }

      if (row.price > this.employee.quota){
        this.$Notify({type: 'danger', message: '余额不足，请选择其他套餐！'});
        return false;
      }

      this.showModal = true;

      this.formData= {
        userId: JSON.parse(sessionStorage.getItem("userInfo")).userId, //申请人标识
        comboId: row.id,
        price: row.price,
        date: null,
        periodId: null,
        periodName: null,
        remark: null
      };

    },
    //取消
    cancel(){
      this.showModal = false;
    },
    //提交
    submit(){

      if (this.formData.date == null || this.formData.date == ""){
        this.$Notify({type: 'danger', message: '请选择预约时间'});
        return false;
      }
      if (this.formData.periodId == null || this.formData.periodId == ""){
        this.$Notify({type: 'danger', message: '请选择预约时间段'});
        return false;
      }

      addBooking(this.formData).then(response => {
        //预约同济医院的时候，预约成功后提示请继续到同济医院健康管理中心微信小程序进行预约
        if(this.detailDate.name.includes("同济")){
          // this.$Notify({
          //   type: 'success',
          //   message: '请继续到同济医院健康管理中心微信小程序进行预约！',
          //   duration: 5000  // 单位是毫秒，5000ms = 5s
          // });

          Dialog.alert({
            message: '请继续到同济医院健康管理中心微信小程序进行预约！',
          }).then(() => {
            // on close
          });

        }else{
          this.$Notify({
            type: 'success',
            message: '预约成功',
            duration: 5000  // 单位是毫秒，5000ms = 5s
          });
        }
        this.showModal = false;

        // 携带数据去体检预约管理
        this.$router.push("/health/applyBooking");
      });

    },


    //详情
    handleDetail(item){
      this.$router.push({
        path: "/health/applyComboDetail",
        query: {
          id: item.id,
        }
      })
    },
    //跳转详情页
    jumpToDetail() {
      this.$router.push({
        path: "/health/applyOrganizationsDetails",
        query: {
          id: this.$route.query.id,
        }
      })
    },

    //选择预约时间
    selectData(item) {
      // console.log(item)
      // console.log(item.format("yyyy-MM-dd"))

      this.formData.date = item.format("yyyy-MM-dd");
      this.showData = false;

    },
    //选择预约时间段
    selectPeriod(option){
      this.formData.periodId = option.id;
      this.formData.periodName = option.name;
      this.showPeriod = false;
    },

    calcMargin(index) {
      if (index == 0) {
        return '10px 0px';
      } else {
        return '0px 0px 10px 0px';
      }
    },

  },
}
</script>
<style lang="less" scoped>
.update-box {
  width: 270px;
}
.sign-box {
  padding: 10px;
  box-sizing: border-box;
}

.showUpdateWin-btn {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;

  .btn {
    margin: 0 5px;
  }
}

.finish-btn {
  position: absolute;
  right: 0;
  bottom: -8px;

  :last-child {
    color: #fff !important;
  }

  .btn {
    margin: 0 5px;
  }
}
.my-swipe {
  height: 160px;

  .van-swipe-item {
    background-color: #b8bab4;
    text-align: center;

    .swiper-img {
      width: 100%;
      height: 160px;
    }
  }
}

.van-ellipsis {
  color: #9e9e9f;
  height: 32px;
  line-height: 32px;

  span:first-child {
    font-size: 14px;
  }

  span:last-child {
    font-size: 14px;
    color: #504f4f;
  }

  .btn {
    margin: 0 5px;
    width: 40px;
  }

  :last-child {
    color: #fff !important;
    padding-left: 7px;
  }

}

.apply-des {
  width: 300px;
  padding: 5px;

  .title {
    text-align: center;
    font-size: 16px;
    line-height: 40px;
  }

  .des {
    border: 1px solid #f2f2f2;
  }
}

.body_box {
  background-color: #f9f9f9;
  padding: 10px 0;


  .fContentMain {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgb(254, 255, 255);
    color: rgb(51, 51, 51);
    border: 0.1px solid rgb(220, 223, 230);
    border-radius: 6px;
    margin: 10px;
    padding: 10px;

    .contentC {
      display: flex;
      align-items: flex-start;
      background: rgb(255, 255, 255);
      color: rgb(51, 51, 51);
      height: 100%;
      flex-direction: column;
      justify-content: space-between;

      .box-title {
        font-weight: bold;
        margin: 5px 10px;
      }


      .title {
        display: flex;
        width: 100%;
        margin: 5px 10px;


        div:first-child {
          width: 100px;
          color: #878888;
        }

        div:last-child {
          width: calc(100% - 100px);
          //font-weight: 500;
        }

      }
    }
  }

  .firstClass {
    min-height: 120px !important;
  }

  .firstClass_02 {
    min-height: 60px !important;
  }

  .secondClass {
    min-height: 60px !important;
  }


  .head {
    display: flex;
    align-items: center;
    justify-content: space-around;
    width: 100%;
    padding: 10px 0px;
    background-color: rgb(254, 255, 255);
    flex-direction: column;

    .picStyle {
      background-image: url('../../../../assets/images/shz.png');
      background-repeat: no-repeat;
      background-size: 100% 100%;
      height: 50px;
      width: 50px;
    }

    .picStyleNoPass {
      background-image: url('../../../../assets/images/prohibit.png');
      background-repeat: no-repeat;
      background-size: 100% 100%;
      height: 50px;
      width: 50px;
    }

    .picStyleWait {
      background-image: url('../../../../assets/images/allocate.png');
      background-repeat: no-repeat;
      background-size: 100% 100%;
      height: 50px;
      width: 50px;
    }

    .picStylePass {
      background-image: url('../../../../assets/images/complete.png');
      background-repeat: no-repeat;
      background-size: 100% 100%;
      height: 50px;
      width: 50px;
    }

    .f {
      font-size: 20px;
      font-weight: 600;
    }

    .ff {
      font-size: 10px;
      font-weight: 600;
    }
  }
}</style>
